package cn.liberals.pet.controller;

import cn.liberals.basic.exception.BusinessException;
import cn.liberals.basic.jwt.UserInfo;
import cn.liberals.basic.utils.LoginContext;
import cn.liberals.org.domain.Employee;
import cn.liberals.org.service.IEmployeeService;
import cn.liberals.pet.dto.HandlerMsgDto;
import cn.liberals.pet.service.ISearchMasterMsgService;
import cn.liberals.pet.domain.SearchMasterMsg;
import cn.liberals.pet.query.SearchMasterMsgQuery;
import cn.liberals.basic.utils.AjaxResult;
import cn.liberals.basic.utils.PageList;
import cn.liberals.user.domain.Logininfo;
import cn.liberals.user.domain.User;
import cn.liberals.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.List;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    public ISearchMasterMsgService searchMasterMsgService;

    @Autowired
    public IUserService userService;

    @Autowired
    IEmployeeService employeeService;


    /**
     * 保存和修改公用的
     *
     * @param searchMasterMsg 传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody SearchMasterMsg searchMasterMsg) {
        try {
            if (searchMasterMsg.getId() != null)
                searchMasterMsgService.update(searchMasterMsg);
            else
                searchMasterMsgService.save(searchMasterMsg);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！" + e.getMessage());
        }
    }

    /**
     * 删除对象信息
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            searchMasterMsgService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！" + e.getMessage());
        }
    }

    //获取用户
    @GetMapping("/{id}")
    public SearchMasterMsg get(@PathVariable("id") Long id) {
        return searchMasterMsgService.loadById(id);
    }


    /**
     * 查看所有的员工信息
     *
     * @return
     */
    @GetMapping()
    public List<SearchMasterMsg> list() {

        return searchMasterMsgService.loadAll();
    }


    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/list")
    public PageList<SearchMasterMsg> json(@RequestBody SearchMasterMsgQuery query) {
        return searchMasterMsgService.queryData(query);
    }

    /* 
    * @Title: publish
    * @Description: 布寻主消息
    * @Author: liberals
    * @Version: 1.0
    * @Date:  2022/7/20 16:53  
    * @Parameters: 
    * @Return 
    */
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg searchMasterMsg, HttpServletRequest req){
        try {
            // 1.给寻主消息的userId设置值
            Logininfo logininfo = LoginContext.getLogininfo(req);
            User user = userService.loadByLogininfoId(logininfo.getId());
            searchMasterMsg.setUserId(user.getId());
            // 2.调用业务方法进行寻主消息发布
            searchMasterMsgService.publish(searchMasterMsg,user);
            return AjaxResult.me();
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }

    /* 
    * @Title: toAudit
    * @Description: 查询待审核消息
    * @Author: liberals
    * @Version: 1.0
    * @Date:  2022/7/20 13:36  
    * @Parameters: 
    * @Return 
    */
    @PostMapping("/toAudit")
    public PageList<SearchMasterMsg> toAudit(@RequestBody SearchMasterMsgQuery query, HttpServletRequest req){
        // 1.查询出user用户
        query.setState(0);
        return searchMasterMsgService.queryData(query);
    }

    /* 
    * @Title: toHandle
    * @Description: 查询待处理消息
    * @Author: liberals
    * @Version: 1.0
    * @Date:  2022/7/20 13:55  
    * @Parameters: 
    * @Return 
    */
    @PostMapping("/toHandle")
    public PageList<SearchMasterMsg> toHandle(@RequestBody SearchMasterMsgQuery query, HttpServletRequest req){
        // 1.设置查询状态为审核通过
        query.setState(1);
        // 2.设置当前查询用户对应的店铺Id
        Logininfo logininfo = LoginContext.getLogininfo(req);
        Employee employee = employeeService.loadByLogininfoId(logininfo.getId());
        query.setShopId(employee.getShopId());
//        System.out.println(query);
        return searchMasterMsgService.queryData(query);
    }

    /* 
    * @Title: pool
    * @Description: 查询寻主池消息
    * @Author: liberals
    * @Version: 1.0
    * @Date:  2022/7/20 13:59  
    * @Parameters: 
    * @Return 
    */
    @PostMapping("/pool")
    public PageList<SearchMasterMsg> pool(@RequestBody SearchMasterMsgQuery query, HttpServletRequest req){
        // 1.设置查询状态为审核通过
        query.setState(1);
        return searchMasterMsgService.queryData(query);
    }

    /*
    * @Title: finish
    * @Description: 查询已完成的寻主消息
    * @Author: liberals
    * @Version: 1.0
    * @Date:  2022/7/20 14:00
    * @Parameters:
    * @Return
    */
    @PostMapping("/finish")
    public PageList<SearchMasterMsg> finish(@RequestBody SearchMasterMsgQuery query, HttpServletRequest req){
        // 1.设置查询状态为审核通过
        query.setState(2);
        return searchMasterMsgService.queryData(query);
    }

    /*
    * @Title: user
    * @Description: 前台用户查询寻主列表
    * @Author: liberals
    * @Version: 1.0
    * @Date:  2022/7/20 16:57
    * @Parameters:
    * @Return
    */
    @PostMapping("/user")
    public PageList<SearchMasterMsg> user(@RequestBody SearchMasterMsgQuery query, HttpServletRequest req){
        // 1.查询出user用户
        Logininfo logininfo = LoginContext.getLogininfo(req);
        User user = userService.loadByLogininfoId(logininfo.getId());
        query.setUserId(user.getId());
        return searchMasterMsgService.queryData(query);
    }

    /* 
    * @Title: accept
    * @Description: 店铺员工接单
    * @Author: liberals
    * @Version: 1.0
    * @Date:  2022/7/20 20:21  
    * @Parameters: 
    * @Return 
    */
    @PostMapping("/accept")
    public AjaxResult accept(@RequestBody HandlerMsgDto handlerMsgDto) {
        try {
            searchMasterMsgService.accept(handlerMsgDto);
            // 2.调用业务方法进行寻主消息发布
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }

    /*
    * @Title: reject
    * @Description: 拒单接口
    * @Author: liberals
    * @Version: 1.0
    * @Date:  2022/7/20 20:07
    * @Parameters:
    * @Return
    */
    @PostMapping("/reject/{msgId}")
    public AjaxResult reject(@PathVariable("msgId") Long msgId){
        try {
            searchMasterMsgService.reject(msgId);
            return AjaxResult.me();
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }

}

